﻿Imports AutoCAD

Public Class CadDrawServiceImpl
    Implements ICadDrawService

    Public Const bulge_4_1 = 0.41421356237 ' 四分之一凸度
    Public Const bulge_2_1 = 1 '二分之一凸度
    Public Shared ZeroPoint As Double() = New Double() {0, 0, 0} '零点
    Public Shared XAxis As Double() = New Double() {1, 0, 0} 'X轴
    Public Shared YAxis As Double() = New Double() {0, 1, 0} 'Y轴
    Public Shared ZAxis As Double() = New Double() {0, 0, 1} 'Z轴


    Public Function DrawPL(ByRef ms As IAcadBlock, ByRef coordinates() As Double) As AcadLWPolyline Implements ICadDrawService.DrawPL
        Return ms.AddLightWeightPolyline(coordinates)
    End Function

    Public Function DrawRec(ByRef ms As IAcadBlock, sx As Double, sy As Double) As AcadLWPolyline Implements ICadDrawService.DrawRec

        Dim p(7) As Double
        p(0) = 0 : p(1) = 0
        p(2) = sx : p(3) = 0
        p(4) = sx : p(5) = sy
        p(6) = 0 : p(7) = sy

        Dim pl As AcadLWPolyline = DrawPL(ms, p)
        With pl
            .Closed = True
        End With

        Return pl

    End Function

    Public Function DrawSquare(ByRef ms As IAcadBlock, sideLength As Double) As AcadLWPolyline Implements ICadDrawService.DrawSquare

        Dim sx As Double, sy As Double
        sx = sideLength : sy = sideLength

        Dim pl As AcadLWPolyline = DrawRec(ms, sx, sy)
        With pl
            .Closed = True
        End With

        Return pl

    End Function

    Public Function DrawCircle(ByRef ms As IAcadBlock, ByRef diameter As Double) As AcadLWPolyline Implements ICadDrawService.DrawCircle

        Dim radius As Double = Math.Abs(diameter / 2)
        Dim p(5) As Double
        p(0) = 0 : p(1) = radius * -1
        p(2) = 0 : p(3) = radius
        p(4) = 0 : p(5) = p(1)

        Dim pl As AcadLWPolyline = DrawPL(ms, p)
        With pl
            For i = 0 To 1
                .SetBulge(i, bulge_2_1)
            Next i
        End With

        Return pl

    End Function

    Public Function DrawCircle2(ByRef ms As IAcadBlock, ByRef diameter As Double) As AcadCircle Implements ICadDrawService.DrawCircle2
        Return ms.AddCircle(ZeroPoint, diameter / 2)
    End Function

    Public Function CreateBlock(ByRef doc As AcadDocument, name As String) As AcadBlock Implements ICadDrawService.CreateBlock
        Return doc.Blocks.Add(ZeroPoint, name)
    End Function

    Public Sub BlockRename(ByRef blk As AcadBlock, ByVal name As String) Implements ICadDrawService.BlockRename
        blk.Name = name
    End Sub

    Public Function DrawPoint(ByRef ms As IAcadBlock, ByRef p() As Double) As AcadPoint Implements ICadDrawService.DrawPoint
        Return ms.AddPoint(p)
    End Function


End Class
